﻿using System;
using System.Linq;
using System.Web.Mvc;

using Flagwind.Web.Plugins.Hosting;

namespace Flagwind.Web.Plugins
{
	public class Workbench : Flagwind.Plugins.WorkbenchBase
	{
		#region 构造方法

		internal Workbench(ApplicationContext applicationContext) : base(applicationContext)
		{
			//添加插件视图引擎到视图引擎集合中
			this.ViewEngines.Insert(0, new PluginRazorViewEngine(this.PluginContext));
			this.ViewEngines.Insert(1, new PluginWebFormViewEngine(this.PluginContext));

			//替换系统默认的控制器工厂
			this.ControllerFactory = new PluginControllerFactory(this.PluginContext);

			//替换系统默认的虚拟路径提供者
			this.VirtualPathProvider = new PluginVirtualPathProvider(this.PluginContext);
		}

		#endregion

		#region 公共属性

		/// <summary>
		/// 获取或设置当前应用的<see cref="IControllerFactory"/>控制器工厂对象。
		/// </summary>
		public IControllerFactory ControllerFactory
		{
			get
			{
				return ControllerBuilder.Current.GetControllerFactory();
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException();

				ControllerBuilder.Current.SetControllerFactory(value);
			}
		}

		/// <summary>
		/// 获取当前应用的<see cref="ViewEngineCollection"/>视图引擎集合。
		/// </summary>
		public ViewEngineCollection ViewEngines
		{
			get
			{
				return System.Web.Mvc.ViewEngines.Engines;
			}
		}

		/// <summary>
		/// 获取或设置当前应用的<see cref="System.Web.Hosting.VirtualPathProvider"/>虚拟路径提供者。
		/// </summary>
		public System.Web.Hosting.VirtualPathProvider VirtualPathProvider
		{
			get
			{
				return System.Web.Hosting.HostingEnvironment.VirtualPathProvider;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException();

				System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(value);
			}
		}

		#endregion

		#region 重写方法

		protected override void OnStart(string[] args)
		{
			// 替换过滤器提供程序
			System.Web.Mvc.FilterProviders.Providers.Add(new PluginFilterProvider(this.PluginContext));

			// 替换模型验证提供程序
			var validator = ModelValidatorProviders.Providers.OfType<DataAnnotationsModelValidatorProvider>().FirstOrDefault();
			if(validator != null)
				System.Web.Mvc.ModelValidatorProviders.Providers.Remove(validator);
			System.Web.Mvc.ModelValidatorProviders.Providers.Add(new PluginModelValidatorProvider(this.PluginContext));

			// 替换系统默认的服务
			System.Web.Http.GlobalConfiguration.Configuration.Services.Replace(typeof(System.Web.Http.Dispatcher.IHttpControllerSelector), new PluginHttpControllerSelector(this.PluginContext));
			System.Web.Http.GlobalConfiguration.Configuration.Services.Replace(typeof(System.Web.Http.Controllers.IHttpActionSelector), new PluginHttpControllerActionSelector());

			// 更改序列化器的默认设置
			System.Web.Http.GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
			var contractResolver = System.Web.Http.GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver as Newtonsoft.Json.Serialization.DefaultContractResolver;
			if(contractResolver != null)
				contractResolver.IgnoreSerializableAttribute = true;

			// 调用基类同名方法，以启动工作台下Startup下的所有工作者
			base.OnStart(args);
		}

		#endregion
	}
}
